package com.yisu.expressway.manager.core.service.impl;

import com.google.common.collect.Lists;
import com.yisu.expressway.ewf.common.web.EasyUIGrid;
import com.yisu.expressway.ewf.entity.GoodsArchives;
import com.yisu.expressway.ewf.entity.GoodsCategory;
import com.yisu.expressway.manager.core.mapper.GoodsArchivesMapper;
import com.yisu.expressway.manager.core.mapper.GoodsCategoryMapper;
import com.yisu.expressway.manager.core.service.IGoodsArchivesService;
import com.yisu.expressway.manager.domain.request.GoodsArchivesRequest;
import com.yisu.expressway.manager.domain.request.SalesGoodsRequest;
import com.yisu.expressway.manager.domain.vo.RecommendGoodsForSelVo;
import com.yisu.expressway.manager.domain.vo.SalesGoodsVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by xlt on 2017/4/24.
 */
@Service
public class GoodsArchivesServiceImpl implements IGoodsArchivesService {
    @Resource
    private GoodsArchivesMapper goodsArchivesMapper;

    @Resource
    private GoodsCategoryMapper goodsCategoryMapper;

    /**
     * 通过商品编号获取商品信息
     *
     * @param goodsNo 商品编号
     * @return
     */
    @Override
    public GoodsArchives getGoodsArchivesByGoodsNo(Long goodsNo) {
        return goodsArchivesMapper.selectByPrimaryKey(goodsNo);
    }

    /**
     * 获取所有商品档案信息
     *
     * @return
     */
    @Override
    public List<GoodsArchives> getAllGoodsData() {
        return goodsArchivesMapper.getAllGoodsData();
    }

    /**
     * 更新商品档案图片信息
     *
     * @param goodsArchives
     * @return
     */
    @Override
    public boolean updateImageInfo(GoodsArchives goodsArchives) {
        return goodsArchivesMapper.updateImageInfo(goodsArchives);
    }

    /**
     * 编辑商品档案信息
     *
     * @param goodsArchives 商品档案
     * @return 成功，失败
     */
    @Override
    public boolean modifyGoodsArchives(GoodsArchives goodsArchives) {
        try {
            GoodsArchives entity = goodsArchivesMapper.selectByPrimaryKey(goodsArchives.getGoodsNo());
            if (entity != null) {
                goodsArchivesMapper.updateByPrimaryKeyWithBLOBs(goodsArchives);
            } else {
                //通过当前分类编号获取最大排序
                Integer maxSort = goodsArchivesMapper.getMaxSortByCategoryCode(goodsArchives.getCategoryCode());
                maxSort = maxSort + 1;
                goodsArchives.setSort(maxSort);
                goodsArchivesMapper.insert(goodsArchives);
            }
            return true;
        } catch (Exception ex) {
            return false;
        }

    }

    /**
     * 通过类目编号分页获取商品档案
     *
     * @param goodsArchivesRequest 分页请求信息
     * @return 商品档案分页列表
     */
    @Override
    public EasyUIGrid<GoodsArchives> getGoodsArchivesByCategoryCodeByPage(GoodsArchivesRequest goodsArchivesRequest) {
        java.util.List<GoodsArchives> goodsArchives = goodsArchivesMapper.getGoodsArchivesByCategoryCodeByPage(goodsArchivesRequest);
        EasyUIGrid<GoodsArchives> gridData = new EasyUIGrid<>();
        gridData.setTotal(goodsArchivesRequest.getTotalRecords());
        gridData.setRows(goodsArchives);
        return gridData;
    }

    /**
     * 编辑商品排序
     *
     * @param goodsArchivesRequest 需要排序的商品信息
     * @return 成功失败
     */
    @Override
    public boolean updateCategoryGoodsSort(GoodsArchivesRequest goodsArchivesRequest) {
        return goodsArchivesMapper.updateCategoryGoodsSort(goodsArchivesRequest) > 0;
    }

    /**
     * 获取当前类目下所有类目商品，包含子分类
     *
     * @param goodsArchivesRequest 请求信息
     * @return 类目商品列表
     */
    @Override
    public EasyUIGrid<RecommendGoodsForSelVo> getGoodsArchivesByParentCategoryCodeForAllByPage(GoodsArchivesRequest goodsArchivesRequest) {
        GoodsCategory entity = goodsCategoryMapper.getGoodsCategoryByGoodsCategoryCode(goodsArchivesRequest.getCategoryCode());
        java.util.List<RecommendGoodsForSelVo> goodsArchives = Lists.newArrayList();
        if (entity != null) {
            if (entity.getCategoryName().equals("首页")) {
                //是首页，获取所有分类商品列表
                goodsArchives = goodsArchivesMapper.selectGoodsArchivesForAllByPage(goodsArchivesRequest);
            } else {
                goodsArchives = goodsArchivesMapper.getGoodsArchivesByParentCategoryCodeForAllByPage(goodsArchivesRequest);
            }
        }
        EasyUIGrid<RecommendGoodsForSelVo> gridData = new EasyUIGrid<>();
        gridData.setTotal(goodsArchivesRequest.getTotalRecords());
        gridData.setRows(goodsArchives);
        return gridData;
    }

    /**
     * 获取售卖商品列表
     *
     * @param salesGoodsRequest 请求信息
     * @return 商品列表
     */
    @Override
    public EasyUIGrid<SalesGoodsVo> getSalesGoodsListByPage(SalesGoodsRequest salesGoodsRequest) {
        List<SalesGoodsVo> salesGoodsVos=goodsArchivesMapper.getSalesGoodsListByPage(salesGoodsRequest);
        EasyUIGrid<SalesGoodsVo> gridData = new EasyUIGrid<>();
        gridData.setTotal(salesGoodsRequest.getTotalRecords());
        gridData.setRows(salesGoodsVos);
        return gridData;
    }

    /**
     * 编辑商品状态
     *
     * @param goodsArchivesRequest 参数信息
     * @return 成功失败
     */
    @Override
    public boolean updateStatus(GoodsArchivesRequest goodsArchivesRequest) {
        return goodsArchivesMapper.updateStatus(goodsArchivesRequest)>0;
    }
}
